package com.medicine.web.controller.manage;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.medicine.common.core.domain.entity.SysUser;
import com.medicine.manage.domain.OrderProcess;
import com.medicine.manage.service.IExpressService;
import com.medicine.manage.service.IOrderConfirmService;
import com.medicine.manage.service.IOrderProcessService;
import com.medicine.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.medicine.common.annotation.Log;
import com.medicine.common.core.controller.BaseController;
import com.medicine.common.core.domain.AjaxResult;
import com.medicine.common.enums.BusinessType;
import com.medicine.manage.domain.Order;
import com.medicine.manage.service.IOrderService;
import com.medicine.common.utils.poi.ExcelUtil;
import com.medicine.common.core.page.TableDataInfo;

/**
 * 订单Controller
 *
 * @author admin
 * @date 2024-10-13
 */
@RestController
@RequestMapping("/manage/order")
public class OrderController extends BaseController
{
    @Autowired
    private IOrderService orderService;

    @Autowired
    private IOrderProcessService orderProcessService;

    @Autowired
    private IOrderConfirmService orderConfirmService;

    @Autowired
    private IExpressService expressService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Order order)
    {
        startPage();
        List<Order> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }

    @GetMapping("/appList")
    public TableDataInfo appList(Order order)
    {
        System.out.println(order);
        List<Order> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('manage:order:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Order order)
    {
        List<Order> list = orderService.selectOrderList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        util.exportExcel(response, list, "订单数据");
    }

    @GetMapping("/selectUserInfo")
    public AjaxResult selectUserInfo(SysUser user){
        if(user.getUserId()==null || user.getUserId()==0L){
            return success(user);
        }
        return success(userService.selectUserById(user.getUserId()));
    }

    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(orderService.selectOrderById(id));
    }

    /**
     * 新增订单
     */
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Order order)
    {
        orderService.insertOrder(order);

        for (int i = 1; i <=5 ; i++) {
            OrderProcess orderProcess = new OrderProcess();
            orderProcess.setOrderId(order.getId());
            orderProcess.setProcessType(new Long(i));
            orderProcessService.insertOrderProcess(orderProcess);
        }
        return success(order);
    }

    /**
     * 修改订单
     */
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Order order)
    {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除订单
     */
    @PreAuthorize("@ss.hasPermi('manage:order:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderService.deleteOrderByIds(ids));
    }

    /**
     *
     * @return
     */
    @GetMapping("end/{id}")
    public AjaxResult OrderFinish(@PathVariable("id") Long id){
        System.out.println(id);
        OrderProcess orderProcess = orderProcessService.selectOrderProcessByOrderAndType(id, IOrderProcessService.EXPRESS);
        if(null == orderProcess){
            return error("不存在物流信息");
        }
        orderProcess.setEndTime(new Date());
        orderProcess.setStatus(1L);

        orderProcessService.updateOrderProcess(orderProcess);

        Order order = new Order();
        order.setId(id);
        order.setStatus(6L);
        return toAjax(orderService.updateOrder(order));


    }

    @GetMapping("/selectUser")
    public AjaxResult list(SysUser user){
        if(user.getNickName()==null || user.getNickName().isEmpty()){
            return success(new ArrayList<SysUser>());
        }
        return success(userService.selectUserLikeByNickName(user.getNickName()));
    }

    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/close")
    public AjaxResult closeOrder(@RequestBody Order order)
    {
        Order order1 = orderService.selectOrderById(order.getId());
        order1.setStatus(100L);

        return toAjax(orderService.updateOrder(order1));
    }
}
